#!/usr/bin/env python
from dict.drugs_dict import drugs
from dict.virus_dict import virus
from dict.desease_dict import deseases
from dict.bacteria_dict import bacterias
from dict.cell_dict import cells
from dict.enzime_dict import enzime
from dict.fungi_dict import fungis
from dict.scince_dict import scince
from dict.human_part_dict import humans_part
from dict.measure_dict import measures
from dict.operation_dict import operations
import string
import os
import time


__author__ = 'gogo'

__version__ = '0.0.1'


class WikiBdaDetector(object):
    """
    class for object detection and wiki/bda link extraction
    """
    def __init__(self, stop_words = None):
        if stop_words is None:
            from dict.eng_stop_words import stop_words
            self.stop_words = stop_words

    # table = string.maketrans("", "")
    # exclude = set(string.punctuation)

    def __clear_punctuation(self, user_string):
        """
        clear punctuation and convert to lower case
        :param user_string:
        """
        # return user_string.translate(self.table, string.punctuation)
        # return ''.join(ch for ch in user_string if ch not in self.exclude)
        for c in string.punctuation:
            user_string = user_string.replace(c, " ")

        return user_string.lower()

    def __remove_stop_words(self, my_string):
        not_stop_words = []
        if my_string is None:
            return None
        for word in my_string.split():
            if word not in self.stop_words:
                not_stop_words.append(word)

        return not_stop_words

    def __get_bda_link(self, drug, ltype=1):
        fl = drug.strip()[0].upper()
        links = []
        with open('bda' + str(ltype) + '/' + fl + '.txt') as f:
            for line in f:
                part = line.strip().split('||')
                if drug.strip().lower() in part[0].lower().strip():
                    links.append(([part[0].strip()], part[2].strip()))
            return links

    def __get_wiki_link(self,word , word_type=None ):
        if word_type == 'virus':
            'grep -q ^' + word.capitalize() + '$' + ' /home/gogo/PycharmProjects/se_test/wikippages'
        else:
            grep = 'grep -q ^' + word.capitalize() + '$' + ' /home/gogo/PycharmProjects/se_test/wikippages'
        r = os.system(grep)
        if r == 0:
            return word.capitalize()
        else:
            return None

    def detect_object(self, user_string):
        my_string = self.__clear_punctuation(user_string)
        bag_words = self.__remove_stop_words(my_string)
        # print bag_words
        if len(bag_words) == 0:
            # print 0
            return None
        elif len(bag_words) == 1:
            # print drugs, bag_words[0]
            word = bag_words[0]
            if word in drugs:
                wtype = 'drug'
                # links = self.__get_bda_link(word)
                # wiki = self.__get_wiki_link(word)
                #return wtype, wiki, links
            elif word in virus:
                wtype = 'virus'
            elif word in deseases:
                wtype = 'desease'
            elif word in bacterias:
                wtype = 'bacteria'
            elif word in cells:
                wtype = 'cell'
            elif word in enzime:
                wtype = 'enzime'
            elif word in fungis:
                wtype = 'fungi'
            elif word in scince:
                wtype = 'science'
            elif word in humans_part:
                wtype = 'human_part'
            elif word in measures:
                wtype = 'measure'
            elif word in operations:
                wtype = 'operation'

            wiki = self.__get_wiki_link(word, virus)

            return wtype, wiki
                #return bag_words

def main():

    start_time = time.time()

    wbd = WikiBdaDetector()

    queries = {
        'in the aspirin',
        'aspirin',
        'for the paracetamol',
        'biseptol',
        'Nebivolol',
        'Panadol',
        'taLcid?',
        'AIDS',
        'ebola',
        'ZIKA',
        'erethism',
        'leukoderma',
        'monodactylism',
        'abiotrophia',
        'ahrensia',
        'kribbella',
        'micromonospora',
        'oxalicibacterium',
        'acanthocyte',
        'blastocyte',
        'hepatocyte',
        'macropromyelocyte',
        'siderocyte',
        'spermatocyte',
        'thrombocyte',
        'vercyte'
    }

    for q in queries:
        print q, '->', wbd.detect_object(q)

    end_time = time.time()
    print end_time -start_time

if __name__ == '__main__':
    main()